package threadPool;

import java.util.concurrent.*;
import java.util.function.BiConsumer;

public class ThreadPool {
    private static ExecutorService executorService = Executors.newSingleThreadExecutor();

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("开始主线程...");
//      testExecutorService();测试ExecutorService创建线程池
//      creatThreadPool7Params();测试自定义一个线程池 7大参数
//      testFutureResultGet();测试有返回值的异步任务 获取返回值时阻塞等待

//        createAsyncTaskByCompletableFuture();测试用任务编排来开启一个简单任务
          testCompletableFutureWhenCompleted();
        testCompletableFutureHandle();
        System.out.println("结束主线程...");
    }

    private static void testCompletableFutureHandle() throws ExecutionException, InterruptedException {
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("1" + Thread.currentThread().getName());
            System.out.println("简单异步编排创建任务...");
            return "你好";
        }, executorService).handleAsync((v, t) -> {
            System.out.println("2" + Thread.currentThread().getName());
            return v + "世界";
        }, executorService);
        System.out.println(future.get());
    }

    private static void testCompletableFutureWhenCompleted() {
        CompletableFuture.runAsync(() -> {
            System.out.println("简单异步编排创建任务...");
//            int i=10/0;
            System.out.println("async:"+Thread.currentThread().getName());
        },executorService).whenComplete((unused, throwable) -> {
            System.out.println(Thread.currentThread().getName());
            if(unused!=null){{
                System.out.println("带参数处理的二阶同步任务");
            }}else {
                System.out.println("不带参数处理的二阶同步任务");
            }
            if(throwable!=null) System.out.println("处理异常");
        });
    }

    private static void createAsyncTaskByCompletableFuture() {
        CompletableFuture.runAsync(() -> {
            System.out.println("简单异步编排创建任务...");
        },executorService);
    }

    private static void testFutureResultGet() throws ExecutionException, InterruptedException {
        Future<String> submit = executorService.submit(() -> {
            System.out.println("子任务开始...");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "result";
        });
        System.out.println(submit.get());
    }

    private static void creatThreadPool7Params() {//5 + 5 + 5
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(5
                , 10
                , 10
                , TimeUnit.SECONDS
                , new LinkedBlockingQueue<>(5)
                , Executors.defaultThreadFactory()
                , new ThreadPoolExecutor.AbortPolicy());
        for (int i = 0; i < 16; i++) {
            int finalI = i;
            poolExecutor.submit(() -> {
                System.out.println("第" + finalI + "开始...");
                return finalI;
            });
        }
    }

    private static void testExecutorService() {
        executorService.execute(() -> {
            System.out.println("async");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }
}
